﻿using SCS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SCS.Common.Models;
using SCS.Common;
namespace SCS.Business
{
    public class SMSServices
    {
        IUnitOfWork _unitOfWork;
        public SMSServices()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// <summary>
        /// GetSMSContent
        /// </summary>
        /// <param name="learner">learner</param>
        /// <returns>string</returns>
        public string GetSMSContent(Learner learner)
        {
            DateTime today = DateTime.Now;
            bool first = true;
            bool firstg = true;
            DateTime estimateDate = today.AddDays(-3);
            string temp = "Hôm nay học sinh được ";
            IList<Grade> gradeToday = learner.Grades.Where(m => m.isSent == false && m.createDate.Value.Date == today.Date).ToList();

            IList<Grade> gradeOfPart = learner.Grades.Where(m => m.isSent == false && m.createDate.Value.Date < today.Date && m.createDate.Value.Date > estimateDate.Date).ToList();
            var dateOfGrade = gradeOfPart.GroupBy(m => m.createDate);
            if (gradeToday.Count == 0)
            {
                temp = string.Empty;
            }
            else
            {
                foreach (var grade in gradeToday)
                {
                    if (first)
                    {
                        temp += (grade.grade1.ToString() + " điểm môn " + grade.Subject.subjectName);
                        first = false;
                    }
                    else
                    {
                        temp += (", " + grade.grade1.ToString() + " điểm môn " + grade.Subject.subjectName);
                    }
                }
                temp += ". ";
            }
            foreach (var b in dateOfGrade)
            {
                firstg = true;
                temp += "Ngày " + b.Key.Value.ToString("d/M/yy") + " Học sinh được ";
                foreach (var grade in gradeOfPart)
                {
                    if (grade.createDate.Value.Date == b.Key.Value.Date)
                    {
                        if (firstg)
                        {
                            temp += (grade.grade1.ToString() + " điểm môn " + grade.Subject.subjectName);
                            firstg = false;
                        }
                        else
                        {
                            temp += (", " + grade.grade1.ToString() + " điểm môn " + grade.Subject.subjectName);
                        }
                    }
                }
                temp += ". ";
            }
            IList<CommentInClass> comment = learner.CommentInClasses.ToList();
            foreach (var cmt in comment)
            {
                temp += cmt.comment;

            }
            return temp;
        }

        /// <summary>
        /// GetSMSAClass
        /// </summary>
        /// <param name="classID">classID</param>
        /// <returns>sms list</returns>
        public List<SMS> GetSMSAClass(int classID)
        {
            string commonCommentText = string.Empty;
            //bool first = true;
            Class classToGet = _unitOfWork.Class.Get(m => m.classID == classID).FirstOrDefault();
            IList<CommonComment> commonComment = classToGet.CommonComments.Where(m => m.dateOfComment.ToShortDateString() == DateTime.Now.ToShortDateString()).ToList();
            foreach (var comment in commonComment)
            {
                commonCommentText += comment.commonComment1 + " ";
            }
            IList<Learner> learnerInClass = _unitOfWork.Learner.Get(m => m.classID == classID).ToList();
            List<SMS> sms = new List<SMS>();
            foreach (var learner in learnerInClass)
            {
                if (learner.parentID != null)
                {
                    SMS smsToLeaner = new SMS();
                    smsToLeaner.learnerName = learner.firstName + " " + learner.lastName;
                    smsToLeaner.phone = learner.Parent.mobile;
                    smsToLeaner.smsContent = StringUtils.convertToUnSign(commonCommentText + GetSMSContent(learner));
                    sms.Add(smsToLeaner);
                }
            }
            return sms;
        }
    }
}
